//
//  HSYBaseVideoPlayerView.m
//  高仿暴走斗图
//
//  Created by key on 16/8/8.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseVideoPlayerView.h"
#import "NSObject+CreateControls.h"
#import "UIView+Gesture.h"
#import "HSYBaseVideoPlayerView+PanGestrues.h"
#import "NSDate+TimeManager.h"

#define OPERATION_VIEW_HEIGHT           50.0f

#define PROGRESS_HEIGHT                 2.5f
#define PROGRESS_VIEW_HEIGHT            40.0f

#define PLAY_BUTTON_OFFSET_LEFT         0.0f
#define PLAY_BUTTON_OFFSET_RIGHT        10.0f
#define PLAY_BUTTON_OFFSET_TOP          PLAY_BUTTON_OFFSET_LEFT
#define PLAY_BUTTON_OFFSET_BOTTOM       PLAY_BUTTON_OFFSET_TOP

#define SCALING_BUTTON_OFFSET_LEFT      10.0f
#define SCALING_BUTTON_OFFSET_RIGHT     0.0f
#define SCALING_BUTTON_OFFSET_TOP       SCALING_BUTTON_OFFSET_RIGHT
#define SCALING_BUTTON_OFFSET_BOTTOM    SCALING_BUTTON_OFFSET_TOP

#define PROGRESS_OFFSET_TOP             2.5f

#define ANIMATION_DURATION              0.3f

#define TIME_LABEL_OFFSET               2.0f
#define TIME_LABEL_HEIGHT               10.0f

#define OPERATION_PROGESS_ALAHP         0.8f

#define ACTIVITYINDICATOR_VIEW_SIZE     50.0f

#define DELAYIN_SECONDS                 6.0f

#define PROGRESS_TINT_COLOR             [UIColor colorWithWhite:1 alpha:0.3]

#define CACHE_PROGESS_VIEW_OFFSET_RIGHT 2.0f
#define CACHE_PROGESS_VIEW_OFFSET_LEFT  5.0f

@interface HSYVideoPlayerProgress : UISlider

@property (nonatomic, assign) CGFloat videoValue;
@property (nonatomic, assign) CGFloat videoMaxValue;
@property (nonatomic, assign) CGRect thumbImageRect;

@end

@implementation HSYVideoPlayerProgress

- (instancetype)initWithFrame:(CGRect)frame minimumValue:(CGFloat)minimumValue maximumValue:(CGFloat)maximumValue andValue:(CGFloat)value minimumTrackTintColor:(UIColor *)minimumTrackTintColor maximumTrackTintColor:(UIColor *)maximumTrackTintColor withThumbImage:(UIImage *)thumbImage
{
    if (self = [super initWithFrame:frame]) {
        
        self.continuous = YES;                                                  //设置只有在离开滑动条的最后时刻才触发滑动事件
        self.minimumValue = minimumValue;                                       //滑动条的最小值
        self.maximumValue = maximumValue;                                       //滑动条的最大值
        self.value = value;                                                     //滑动条的当前值
        
        [self setThumbImage:thumbImage forState:UIControlStateNormal];          //设置静态时的滑块显示图片
        [self setThumbImage:thumbImage forState:UIControlStateHighlighted];     //设置滑动时的滑块显示图片
        
        self.minimumTrackTintColor = minimumTrackTintColor;                     //进度的颜色
        self.maximumTrackTintColor = maximumTrackTintColor;                     //剩余的颜色
    }
    return self;
}

- (void)setVideoValue:(CGFloat)videoValue
{
    _videoValue = videoValue;
    self.value = videoValue;
}

- (void)setVideoMaxValue:(CGFloat)videoMaxValue
{
    _videoMaxValue = videoMaxValue;
    self.maximumValue = videoMaxValue;
}

#pragma mark - Overloading

- (CGRect)trackRectForBounds:(CGRect)bounds
{
    CGRect rect = CGRectMake(CGRectGetMinX(bounds), CGRectGetMidY(bounds) - PROGRESS_HEIGHT/2, CGRectGetWidth(bounds), PROGRESS_HEIGHT);
    return rect;
}

- (CGRect)thumbRect
{
    CGRect thumbImageRect = [self thumbRectForBounds:self.bounds
                                           trackRect:[self trackRectForBounds:self.bounds]
                                               value:self.value];
    self.thumbImageRect = thumbImageRect;
    return thumbImageRect;
}

+ (HSYVideoPlayerProgress *)showProgressForFrame:(CGRect)frame propertyDictionary:(NSDictionary *)dictionary didChangedValueBlock:(void(^)(NSNumber *value))block
{
    CGFloat minValue        = [dictionary[@(kVideoPlayerProgressPropertyTypeMinValue)] floatValue];
    CGFloat maxValue        = [dictionary[@(kVideoPlayerProgressPropertyTypeMaxValue)] floatValue];
    CGFloat value           = [dictionary[@(kVideoPlayerProgressPropertyTypeProgessValue)] floatValue];
    UIColor *minTintColor   = dictionary[@(kVideoPlayerProgressPropertyTypeMinTintColor)];
    UIColor *maxTintColor   = dictionary[@(kVideoPlayerProgressPropertyTypeMaxTintColor)];
    UIImage *thumbImage     = dictionary[@(kVideoPlayerProgressPropertyTypeThumbImage)];
    
    HSYVideoPlayerProgress *progress = [[HSYVideoPlayerProgress alloc] initWithFrame:frame
                                                                        minimumValue:minValue
                                                                        maximumValue:maxValue
                                                                            andValue:value
                                                               minimumTrackTintColor:minTintColor
                                                               maximumTrackTintColor:maxTintColor
                                                                      withThumbImage:thumbImage];
    
    [[[progress rac_signalForControlEvents:UIControlEventValueChanged] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(HSYVideoPlayerProgress *progess) {
        if (block) {
            block(@(progess.value));
        }
    }];
    return progress;
}

@end

@interface HSYVideoPlayerOperationView : UIView

@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *scalingButton;
@property (nonatomic, strong) UILabel *videoTimeLabel;
@property (nonatomic, strong) HSYVideoPlayerProgress *videoProgess;
@property (nonatomic, strong) UIProgressView *cacheProgessView;
@property (nonatomic, copy) void(^didChangedValueBlock)(NSNumber *value);
@property (nonatomic, assign, readonly) BOOL isHide;
@property (nonatomic, assign) CGFloat progess;
@property (nonatomic, assign) CGFloat maxProgess;
@property (nonatomic, strong) UIView *blackMask;

@end

@implementation HSYVideoPlayerOperationView

- (instancetype)initWithFrame:(CGRect)frame maxValue:(CGFloat)maxValue currentValue:(CGFloat)currentValue minTintColor:(UIColor *)minTintColor maxTintColor:(UIColor *)maxTintColor thumbImage:(UIImage *)thumbImage
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self setupForMaxValue:maxValue currentValue:currentValue minTintColor:minTintColor maxTintColor:maxTintColor thumbImage:thumbImage];
    }
    return self;
}

- (void)setupForMaxValue:(CGFloat)maxValue currentValue:(CGFloat)currentValue minTintColor:(UIColor *)minTintColor maxTintColor:(UIColor *)maxTintColor thumbImage:(UIImage *)thumbImage
{
    self.blackMask = [NSObject createViewWithBgColor:[UIColor blackColor]];
    self.blackMask.alpha = OPERATION_PROGESS_ALAHP;
    [self addSubview:self.blackMask];
    [self.blackMask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.playButton = [NSObject createButtonBGImage:nil
                                             hImage:nil
                                              title:nil
                                             hTitle:nil];
    [self.playButton setImage:CREATE_IMG(VIDEO_STOP) forState:UIControlStateNormal];
    [self.playButton setImage:CREATE_IMG(VIDEO_STOP) forState:UIControlStateHighlighted];
    [self addSubview:self.playButton];
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(PLAY_BUTTON_OFFSET_LEFT);
        make.top.equalTo(self.mas_top).offset(PLAY_BUTTON_OFFSET_TOP);
        make.bottom.equalTo(self.mas_bottom).offset(PLAY_BUTTON_OFFSET_BOTTOM);
        make.width.equalTo(self.playButton.mas_height);
    }];
    
    
    @weakify(self);
    self.videoProgess = [HSYVideoPlayerProgress showProgressForFrame:CGRectZero
                                                  propertyDictionary:@{
                                                                       @(kVideoPlayerProgressPropertyTypeMinValue)      : @(0.0f),
                                                                       @(kVideoPlayerProgressPropertyTypeMaxValue)      : @(maxValue),
                                                                       @(kVideoPlayerProgressPropertyTypeProgessValue)  : @(currentValue),
                                                                       @(kVideoPlayerProgressPropertyTypeMinTintColor)  : minTintColor,
                                                                       @(kVideoPlayerProgressPropertyTypeMaxTintColor)  : maxTintColor,
                                                                       @(kVideoPlayerProgressPropertyTypeThumbImage)    : thumbImage,
                                                                       }
                                                didChangedValueBlock:^(NSNumber *value) {
                                                    @strongify(self);
                                                    if (self.didChangedValueBlock) {
                                                        self.didChangedValueBlock(value);
                                                    }
                                                }];
    [self.videoProgess setCenter:CGPointMake(self.videoProgess.center.x, self.playButton.center.y)];
    [self addSubview:self.videoProgess];
    
    self.cacheProgessView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.cacheProgessView.progressTintColor = PROGRESS_TINT_COLOR;
    self.cacheProgessView.trackTintColor = [UIColor clearColor];
    [self insertSubview:self.cacheProgessView belowSubview:self.videoProgess];
    
    self.scalingButton = [NSObject createButtonBGImage:nil
                                                hImage:nil
                                                 title:nil
                                                hTitle:nil];
    [self.scalingButton setImage:CREATE_IMG(VIDEO_SCAGING_OUT) forState:UIControlStateNormal];
    [self.scalingButton setImage:CREATE_IMG(VIDEO_SCAGING_OUT) forState:UIControlStateHighlighted];
    [self addSubview:self.scalingButton];
    
    [self.scalingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-SCALING_BUTTON_OFFSET_RIGHT);
        make.top.equalTo(self.mas_top).offset(SCALING_BUTTON_OFFSET_TOP);
        make.bottom.equalTo(self.mas_bottom).offset(SCALING_BUTTON_OFFSET_BOTTOM);
        make.width.equalTo(self.scalingButton.mas_height);
    }];
    
    [self.videoProgess mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.playButton.mas_right).offset(PLAY_BUTTON_OFFSET_RIGHT);
        make.right.equalTo(self.scalingButton.mas_left).offset(-SCALING_BUTTON_OFFSET_LEFT);
        make.top.equalTo(self.mas_top).offset(PROGRESS_OFFSET_TOP);
        make.bottom.equalTo(self.mas_bottom).offset(-PROGRESS_OFFSET_TOP);
    }];
    
    [self.cacheProgessView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.videoProgess.mas_left).offset(CGRectGetWidth(self.videoProgess.thumbImageRect) + CACHE_PROGESS_VIEW_OFFSET_LEFT);
        make.right.equalTo(self.videoProgess.mas_right).offset(CACHE_PROGESS_VIEW_OFFSET_RIGHT);
        make.centerY.equalTo(self.videoProgess.mas_centerY);
        make.height.equalTo(@(PROGRESS_HEIGHT));
    }];
    
    self.videoTimeLabel = [NSObject createLabelWithText:HSYLOCALIZED(@"00:00:00/00:00:00")
                                              textColor:[UIColor whiteColor] bkColor:[UIColor clearColor]
                                                   font:HSYFONTSIZE(10)
                                          textAlignment:NSTextAlignmentLeft];
    [self addSubview:self.videoTimeLabel];
    [self.videoTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.videoProgess.mas_left).offset(thumbImage.size.width);
        make.height.equalTo(@(TIME_LABEL_HEIGHT));
        make.bottom.equalTo(self.mas_bottom).offset(-TIME_LABEL_OFFSET);
        make.right.equalTo(self.scalingButton.mas_left).offset(-SCALING_BUTTON_OFFSET_LEFT);
    }];
    _isHide = NO;
}

- (void)updateButtonStateForImage:(UIImage *)image isSelected:(BOOL)selected
{
    if (!self.playButton) {
        return;
    }
    [self.playButton setImage:image forState:UIControlStateNormal];
    [self.playButton setImage:image forState:UIControlStateHighlighted];
    self.playButton.selected = selected;
}

- (void)setProgess:(CGFloat)progess
{
    _progess = progess;
    self.videoProgess.videoValue = progess;
}

- (void)setMaxProgess:(CGFloat)maxProgess
{
    _maxProgess = maxProgess;
    self.videoProgess.videoMaxValue = maxProgess;
}

- (NSString *)setTimeLabelValues:(double)currentTime totalTime:(double)totalTime
{
    double hoursElapsed   = floor(currentTime / D_HOUR);
    double minutesElapsed = floor(currentTime / D_MINUTE);
    double secondsElapsed = fmod(currentTime, D_MINUTE);
    NSString *timeElapsedString = [NSString stringWithFormat:@"%02.0f:%02.0f:%02.0f",hoursElapsed, minutesElapsed, secondsElapsed];
    
    double hoursRemaining   = floor(totalTime / D_HOUR);
    double minutesRemaining = floor(totalTime / D_MINUTE);
    double secondsRemaining = floor(fmod(totalTime, D_MINUTE));
    NSString *timeRmainingString = [NSString stringWithFormat:@"%02.0f:%02.0f:%02.0f",hoursRemaining, minutesRemaining, secondsRemaining];
    
    self.videoTimeLabel.text = [NSString stringWithFormat:@"%@/%@",timeElapsedString,timeRmainingString];
    self.progess = currentTime;
    self.maxProgess = totalTime;
    
    return [self.videoTimeLabel.text mutableCopy];
}

#pragma mark - Animation

- (void)animation
{
    if (!self.isHide) {
        [self showAnimation];
    } else {
        [self hideAnimation];
    }
}

- (void)showAnimation
{
    @weakify(self);
    [UIView animateWithDuration:[self duration] animations:^{
        @strongify(self);
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
        _isHide = YES;
        @strongify(self);
        [self dispath_afterHideAnimation];
    }];
}

- (void)hideAnimation
{
    @weakify(self);
    [UIView animateWithDuration:[self duration] animations:^{
        @strongify(self);
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        _isHide = NO;
    }];
}

- (void)dispath_afterHideAnimation
{
    @weakify(self);
    [NSObjcGCD gcd_DelayExecutionForDelayInSeconds:(int64_t)(DELAYIN_SECONDS * NSEC_PER_SEC) dispatchCompletion:^{
        @strongify(self);
        [self hideAnimation];
    }];
}

- (NSTimeInterval)duration
{
    return ANIMATION_DURATION;
}

#pragma mark - Draw

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    [self.cacheProgessView.layer setCornerRadius:self.cacheProgessView.viewHeight/2];
}

#pragma mark - Show

+ (HSYVideoPlayerOperationView *)showOperationViewForProgressProperty:(NSDictionary *)property didChangedValueBlock:(void(^)(NSNumber *value))valueblock playVideoBlock:(void(^)(UIButton *button))playBlock scalingVideoBlock:(void(^)(UIButton *button))scalingBlock
{
    HSYVideoPlayerOperationView *operationView = [[HSYVideoPlayerOperationView alloc]
                                                  initWithFrame:CGRectZero
                                                  maxValue:[property[@(kVideoPlayerProgressPropertyTypeMaxValue)] floatValue]
                                                  currentValue:[property[@(kVideoPlayerProgressPropertyTypeProgessValue)] floatValue]
                                                  minTintColor:property[@(kVideoPlayerProgressPropertyTypeMinTintColor)]
                                                  maxTintColor:property[@(kVideoPlayerProgressPropertyTypeMaxTintColor)]
                                                  thumbImage:property[@(kVideoPlayerProgressPropertyTypeThumbImage)]];
    operationView.didChangedValueBlock = ^(NSNumber *value) {
        if (valueblock) {
            valueblock(value);
        }
    };
    [[[operationView.playButton rac_signalForControlEvents:UIControlEventTouchUpInside] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(UIButton *button) {
        if (playBlock) {
            button.selected = !button.selected;
            if (button.selected) {
                [button setImage:CREATE_IMG(VIDEO_PLAY) forState:UIControlStateNormal];
                [button setImage:CREATE_IMG(VIDEO_PLAY) forState:UIControlStateHighlighted];
            } else {
                [button setImage:CREATE_IMG(VIDEO_STOP) forState:UIControlStateNormal];
                [button setImage:CREATE_IMG(VIDEO_STOP) forState:UIControlStateHighlighted];
            }
            playBlock(button);
        }
    }];
    [[[operationView.scalingButton rac_signalForControlEvents:UIControlEventTouchUpInside] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(UIButton *button) {
        if (scalingBlock) {
            button.selected = !button.selected;
            if (button.selected) {
                [button setImage:CREATE_IMG(VIDEO_SCAGING_IN) forState:UIControlStateNormal];
                [button setImage:CREATE_IMG(VIDEO_SCAGING_IN) forState:UIControlStateHighlighted];
            } else {
                [button setImage:CREATE_IMG(VIDEO_SCAGING_OUT) forState:UIControlStateNormal];
                [button setImage:CREATE_IMG(VIDEO_SCAGING_OUT) forState:UIControlStateHighlighted];
            }
            scalingBlock(button);
        }
    }];
    return operationView;
}

@end

@interface HSYBaseVideoPlayerView ()<UIGestureRecognizerDelegate>

@property (nonatomic, copy) void(^didChangedValueBlock)(NSNumber *value);                       //滑块改变的回调
@property (nonatomic, copy) void(^didPlayVideoBlock)(UIButton *button);                         //点击了播放按钮
@property (nonatomic, copy) void(^didScalingVideoBlock)(UIButton *button);                      //点击了放缩按钮

@property (nonatomic, strong) HSYVideoPlayerOperationView *operationView;                       //底部的操作视图
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;

@end

@implementation HSYBaseVideoPlayerView

- (instancetype)initWithFrame:(CGRect)frame progressProperty:(NSDictionary *)progressProperty
{
    if (self = [super initWithFrame:frame]) {
        [self setupForProgressProperty:progressProperty];
    }
    return self;
}

- (void)setupForProgressProperty:(NSDictionary *)progressProperty
{
    _clearMask = [NSObject createViewWithBgColor:[UIColor clearColor]];
    [self addSubview:self.clearMask];
    [self.clearMask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    @weakify(self);
    self.operationView = [HSYVideoPlayerOperationView showOperationViewForProgressProperty:progressProperty didChangedValueBlock:^(NSNumber *value) {
        
        @strongify(self);
        if (self.didChangedValueBlock) {
            [self.operationView updateButtonStateForImage:CREATE_IMG(VIDEO_STOP) isSelected:NO];
            self.didChangedValueBlock(value);
            [self setTimeLabelValues:[value floatValue] totalTime:self.maxValue];
        }
        
    } playVideoBlock:^(UIButton *button) {
        
        @strongify(self);
        if (self.didPlayVideoBlock) {
            self.didPlayVideoBlock(button);
        }
        
    } scalingVideoBlock:^(UIButton *button) {
        
        @strongify(self);
        if (self.didScalingVideoBlock) {
            self.didScalingVideoBlock(button);
        }
        
    }];
    [self addSubview:self.operationView];
    [self.operationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.mas_bottom);
        make.height.equalTo(@(OPERATION_VIEW_HEIGHT));
    }];
    UITapGestureRecognizer *gesture = [UIView rad_addTapGestureRecognizerWithDelegate:self numberOfTouchesRequired:1 numberOfTapsRequired:1 selectBlock:^(id x) {
        @strongify(self);
        [self.operationView animation];
    }];
    [self.clearMask addGestureRecognizer:gesture];
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    panGesture.minimumNumberOfTouches = 1;//最少手指数目
    panGesture.maximumNumberOfTouches = 1;//最多手指数目
    panGesture.delaysTouchesEnded = NO;
    
    [self.clearMask addGestureRecognizer:panGesture];
    [self createIndicatorView];
    [self addActivityIndicatorView];
}

- (void)handlePan:(UIPanGestureRecognizer *)pan
{
    [self handlePanGesture:pan];
}

- (void)createIndicatorView
{
    self.volumeIndicatorView = [[HSYPlayerVolumeView alloc] initWithLogo:CREATE_IMG(VIDEO_VOLUME) highLogo:CREATE_IMG(VIDEO_VOLUME_MUTE)];
    [self addSubview:self.volumeIndicatorView];
    [self.volumeIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@(kVideoTimeIndicatorViewSide));
        make.width.equalTo(self.volumeIndicatorView.mas_height);
    }];
    self.brightnessIndicatorView = [[HSYPlayerBrightnessView alloc] initWithLogo:CREATE_IMG(VIDEO_BRIGHTNESS) highLogo:CREATE_IMG(VIDEO_BRIGHTNESS)];
    [self addSubview:self.brightnessIndicatorView];
    [self.brightnessIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@(kVideoTimeIndicatorViewSide));
        make.width.equalTo(self.volumeIndicatorView.mas_height);
    }];
    self.timeIndicatorView = [[HSYPlayerTimeIndicatorView alloc] initWithLogo:CREATE_IMG(VIDEO_REWIND) highLogo:CREATE_IMG(VIDEO_FASTFORWORD)];
    [self addSubview:self.timeIndicatorView];
    [self.timeIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@(kVideoTimeIndicatorViewSide));
        make.width.equalTo(self.volumeIndicatorView.mas_height);
    }];
}

- (void)addActivityIndicatorView
{
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self addSubview:self.activityIndicatorView];
    [self activityAnimationByIsStartAnimation:YES];
    [self.activityIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@(ACTIVITYINDICATOR_VIEW_SIZE));
        make.height.equalTo(self.activityIndicatorView.mas_width);
    }];
}

- (void)activityAnimationByIsStartAnimation:(BOOL)startAnimating
{
    if (startAnimating) {
        if (![self.activityIndicatorView isAnimating]) {
            [self.activityIndicatorView startAnimating];
        }
    } else {
        if ([self.activityIndicatorView isAnimating]) {
            [self.activityIndicatorView stopAnimating];
        }
    }
}

#pragma mark - Update

- (void)updateVideoPlayState
{
    [self.operationView updateButtonStateForImage:CREATE_IMG(VIDEO_PLAY) isSelected:YES];
}

- (void)updateVideoStopState
{
    [self.operationView updateButtonStateForImage:CREATE_IMG(VIDEO_STOP) isSelected:NO];
}

- (NSString *)setTimeLabelValues:(double)currentTime totalTime:(double)totalTime
{
    _value = currentTime;
    _maxValue = totalTime;
    return [self.operationView setTimeLabelValues:currentTime totalTime:totalTime];
}

- (void)updateConstraint
{
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

#pragma mark - Animation

- (void)showAnimation
{
    if (!self.operationView) {
        return;
    }
    [self.operationView showAnimation];
}

- (void)hideAnimation
{
    if (!self.operationView) {
        return;
    }
    [self.operationView hideAnimation];
}

- (void)dispath_afterHideAnimation
{
    if (!self.operationView) {
        return;
    }
    [self.operationView dispath_afterHideAnimation];
}

#pragma mark - Setter

- (void)setMaxValue:(CGFloat)maxValue
{
    _maxValue = maxValue;
    self.operationView.maxProgess = maxValue;
}

- (void)setCacheProgess:(CGFloat)cacheProgess
{
    _cacheProgess = cacheProgess;
    self.operationView.cacheProgessView.progress = cacheProgess;
}

#pragma mark - Show

+ (HSYBaseVideoPlayerView *)showVideoPlayerViewForProgressProperty:(NSDictionary *)progressProperty frame:(CGRect)frame didChangedValueBlock:(void(^)(NSNumber *value))valueblock playVideoBlock:(void(^)(UIButton *button, CGFloat value))playBlock scalingVideoBlock:(void(^)(UIButton *button))scalingBlock didPanGestrueChangeValueBlock:(void(^)(NSNumber *value))panChangeValueBlock
{
    HSYBaseVideoPlayerView *playerView = [[HSYBaseVideoPlayerView alloc] initWithFrame:frame progressProperty:progressProperty];
    
    @weakify(playerView);
    dispatch_async(dispatch_get_main_queue(), ^{
        @strongify(playerView);
        playerView.didChangedValueBlock = ^(NSNumber *value) {
            if (valueblock) {
                valueblock(value);
            }
        };
        playerView.didPlayVideoBlock = ^(UIButton *button) {
            @strongify(playerView);
            if (playBlock) {
                playBlock(button, playerView.value);
            }
        };
        playerView.didScalingVideoBlock = ^(UIButton *button) {
            if (scalingBlock) {
                scalingBlock(button);
            }
        };
        playerView.didPanGestrueChangeValueBlock = ^(NSNumber *value) {
            if (panChangeValueBlock) {
                panChangeValueBlock(value);
            }
        };
    });
    
    return playerView;
}

@end
